package org.android.brasil.projetos.spacehero.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.input.touch.TouchEvent;
import org.android.brasil.projetos.spacehero.base.BaseGameController;
import org.android.brasil.projetos.spacehero.base.IGameEntity;

public class AndEngineGameController extends BaseGameController implements IOnSceneTouchListener {
	
	private Scene mScene;
	
	private Engine mEngine;
	
	private Camera mCamera;
	
	private AndEngineGameEntityFactory mGameEntityFactory;
	
	@Override
	protected void createGameEntityFactory() {
		
		this.mGameEntityFactory = new AndEngineGameEntityFactory();
		this.setGameEntityFactory(this.mGameEntityFactory);
		super.createGameEntityFactory();
	}
	
	@Override
	protected void addEnemy(IGameEntity pGameEntity) {

		this.mScene.getFirstChild().attachChild(((AndEngineGameEntity)pGameEntity).getEntity());
		super.addEnemy(pGameEntity);
	}
	
	@Override
	protected void removeEnemy(IGameEntity pGameEntity) {

		((AndEngineGameEntity)pGameEntity).getEntity().detachSelf();
		super.removeEnemy(pGameEntity);
	}
	
	@Override
	protected void createShip() {
		
		AndEngineGameEntity gameEntity = this.mGameEntityFactory.createAndEngineDummy();
		this.mScene.getFirstChild().attachChild(gameEntity.getEntity());
		
		this.setShip(gameEntity);
		super.createShip();
	}
	
	@Override
	protected void createEnemy() {
		
		AndEngineGameEntity gameEntity = this.mGameEntityFactory.createAndEngineDummy();
		this.addEnemy(gameEntity);
	}
	
	public AndEngineGameController(){
		super();
	}
	
	public Engine LoadEngine(){

		this.mCamera = new Camera(0, 0, 480, 800);

		final EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(480, 800), this.mCamera);

		this.mEngine = new Engine(engineOptions);
		return this.mEngine;		
	}
	
	public void LoadComplete(){
		
	}
	
	public void LoadResource(){
		
	}
	
	public Scene LoadScene(){
		
		this.mScene = new Scene(1);
		this.mScene.setOnSceneTouchListener(this);
		this.mScene.registerUpdateHandler(new IUpdateHandler(){
			 
	            public void onUpdate(float pSecondsElapsed) {
	            	
	                AndEngineGameController.this.updateGame(pSecondsElapsed);
	            }
	
	            public void reset() {                          
	            }
            });
		
		return this.mScene;
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

		this.setShipPosition(pSceneTouchEvent.getX());
		return true;
	}
}
